package com.LeeCode;

/**
 * 二叉搜索树中的插入操作
 */

public class Code701 {
    public static void main(String[] args) {

    }

    public TreeNode insertIntoBST(TreeNode root, int val) {
        TreeNode parent = null, cur = root;
        while (cur != null) {
            parent = cur;
            if (val < cur.val)
                cur = cur.left;
            else
                cur = cur.right;
        }
        TreeNode newNode = new TreeNode(val);
        if (parent == null) return newNode;

        if (val > parent.val)
            parent.right = newNode;
        else parent.left = newNode;
        return root;
    }
}
